package com.yao.http;

import android.text.TextUtils;
import com.yao.util.PreferenceCache;
import okhttp3.*;

import java.io.IOException;

public class HttpTokenInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        Headers headers = original.headers();
        Request.Builder builder = original.newBuilder();
        builder.addHeader("token", PreferenceCache.getToken());
        if (TextUtils.isEmpty(headers.get("Content-type"))) {
            builder.addHeader("Content-type", "application/json;charset=UTF-8");
        }
        String tag = headers.get("Tag");
        if (!TextUtils.isEmpty(tag)) {
            builder.tag(tag);
        }
        return chain.proceed(builder.build());
    }
}
